package com.pds.web.monitor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pds.common.utils.DateUtils;
import com.pds.web.category.entity.PdsCategory;
import com.pds.web.category.entity.PdsCategoryStandard;
import com.pds.web.category.mapper.PdsCategoryMapper;
import com.pds.web.category.mapper.PdsCategoryStandardMapper;
import com.pds.web.monitor.domain.PdsMonitorTaskRange;
import com.pds.web.monitor.dto.CategoryStandardDTO;
import com.pds.web.monitor.dto.TaskRangeDTO;
import com.pds.web.monitor.mapper.PdsMonitorTaskRangeMapper;
import com.pds.web.monitor.service.IPdsMonitorTaskRangeService;
import com.pds.web.monitor.vo.CategoryStandardVO;
import com.pds.web.monitor.vo.LargeCategoryVO;
import com.pds.web.monitor.vo.SmallCategoryVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class PdsMonitorTaskRangeServiceImpl extends ServiceImpl<PdsMonitorTaskRangeMapper, PdsMonitorTaskRange> implements IPdsMonitorTaskRangeService {

    @Resource
    private PdsMonitorTaskRangeMapper pdsMonitorTaskRangeMapper;

    @Resource
    private PdsCategoryMapper pdsCategoryMapper;

    @Resource
    private PdsCategoryStandardMapper pdsCategoryStandardMapper;

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Integer addTaskRange(TaskRangeDTO taskRangeDTO) {
        List<CategoryStandardDTO> categoryStandardDTOList = taskRangeDTO.getCategoryStandardDTOList();
        int count = 0;
        Date createTime = DateUtils.parseDate(DateUtils.getTime());
        Long taskId = taskRangeDTO.getTaskId();
        Long organizationId = taskRangeDTO.getOrganizationId();
        if (!Objects.isNull(categoryStandardDTOList) && !categoryStandardDTOList.isEmpty()) {
            for (CategoryStandardDTO categoryStandardDTO : categoryStandardDTOList) {
                PdsMonitorTaskRange pdsMonitorTaskRange = new PdsMonitorTaskRange();
                pdsMonitorTaskRange.setCreateTime(createTime);
                pdsMonitorTaskRange.setTaskId(taskId);
                pdsMonitorTaskRange.setOrganizationId(organizationId);
                pdsMonitorTaskRange.setLargeCategoryId(categoryStandardDTO.getLargeCategoryId());
                pdsMonitorTaskRange.setSmallCategoryId(categoryStandardDTO.getSmallCategoryId());
                pdsMonitorTaskRange.setStandardId(categoryStandardDTO.getStandardId());
                pdsMonitorTaskRangeMapper.insert(pdsMonitorTaskRange);
                count = count + 1;
            }
        }
        return count;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Integer deleteTaskRange(List<Long> ids) {
        return pdsMonitorTaskRangeMapper.deleteBatchIds(ids);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Integer updateTaskRange(TaskRangeDTO taskRangeDTO) {
        Long taskId = taskRangeDTO.getTaskId();
        Long organizationId = taskRangeDTO.getOrganizationId();
        QueryWrapper<PdsMonitorTaskRange> qw = new QueryWrapper<>();
        if (!Objects.isNull(taskId)) {
            qw.eq("task_id", taskId);
        }
        if (!Objects.isNull(organizationId)) {
            qw.eq("organization_id", organizationId);
        }
        pdsMonitorTaskRangeMapper.delete(qw);

        List<CategoryStandardDTO> categoryStandardDTOList = taskRangeDTO.getCategoryStandardDTOList();
        int count = 0;
        if (!Objects.isNull(categoryStandardDTOList) && !categoryStandardDTOList.isEmpty()) {
            for (CategoryStandardDTO categoryStandardDTO : categoryStandardDTOList) {
                PdsMonitorTaskRange pdsMonitorTaskRange = new PdsMonitorTaskRange();
                Date createTime = DateUtils.parseDate(DateUtils.getTime());
                pdsMonitorTaskRange.setCreateTime(createTime);
                pdsMonitorTaskRange.setTaskId(taskRangeDTO.getTaskId());
                pdsMonitorTaskRange.setOrganizationId(taskRangeDTO.getOrganizationId());
                pdsMonitorTaskRange.setLargeCategoryId(categoryStandardDTO.getLargeCategoryId());
                pdsMonitorTaskRange.setSmallCategoryId(categoryStandardDTO.getSmallCategoryId());
                pdsMonitorTaskRange.setStandardId(categoryStandardDTO.getStandardId());
                pdsMonitorTaskRangeMapper.insert(pdsMonitorTaskRange);
                count = count + 1;
            }
        }
        return count;
    }

    @Override
    public List<LargeCategoryVO> selectList(PdsMonitorTaskRange pdsMonitorTaskRange) {
        List<LargeCategoryVO> list = new ArrayList<>();
        Long organizationId = pdsMonitorTaskRange.getOrganizationId();
        Long taskId = pdsMonitorTaskRange.getTaskId();
        if (!Objects.isNull(organizationId) && !Objects.isNull(taskId)) {
            QueryWrapper<PdsMonitorTaskRange> largeWrapper = new QueryWrapper<>();
            largeWrapper.eq("organization_id", organizationId);
            largeWrapper.eq("task_id", taskId);
            largeWrapper.groupBy("large_category_id");
            List<PdsMonitorTaskRange> largeList = pdsMonitorTaskRangeMapper.selectList(largeWrapper);
            if (!Objects.isNull(largeList) && !largeList.isEmpty()) {
                for (PdsMonitorTaskRange large : largeList) {
                    Long largeCategoryId = large.getLargeCategoryId();
                    PdsCategory largeCategory = pdsCategoryMapper.selectById(largeCategoryId);
                    LargeCategoryVO largeCategoryVO = new LargeCategoryVO();
                    BeanUtils.copyProperties(largeCategory, largeCategoryVO);
                    list.add(largeCategoryVO);
                }
            }
            if (!list.isEmpty()) {
                for (LargeCategoryVO largeCategoryVO : list) {
                    Long largeCategoryId = largeCategoryVO.getId();
                    QueryWrapper<PdsMonitorTaskRange> smallWrapper = new QueryWrapper<>();
                    smallWrapper.eq("organization_id", organizationId);
                    smallWrapper.eq("task_id", taskId);
                    smallWrapper.eq("large_category_id", largeCategoryId);
                    smallWrapper.groupBy("small_category_id");
                    List<PdsMonitorTaskRange> smallList = pdsMonitorTaskRangeMapper.selectList(smallWrapper);
                    if (!Objects.isNull(smallList) && !smallList.isEmpty()) {
                        for (PdsMonitorTaskRange small : smallList) {
                            Long smallCategoryId = small.getSmallCategoryId();
                            PdsCategory smallCategory = pdsCategoryMapper.selectById(smallCategoryId);
                            SmallCategoryVO smallCategoryVO = new SmallCategoryVO();
                            BeanUtils.copyProperties(smallCategory, smallCategoryVO);
                            QueryWrapper<PdsMonitorTaskRange> standardWrapper = new QueryWrapper<>();
                            standardWrapper.eq("organization_id", organizationId);
                            standardWrapper.eq("task_id", taskId);
                            standardWrapper.eq("large_category_id", largeCategoryId);
                            standardWrapper.eq("small_category_id", smallCategoryId);
                            List<PdsMonitorTaskRange> standardList = pdsMonitorTaskRangeMapper.selectList(standardWrapper);
                            if (!Objects.isNull(standardList) && !standardList.isEmpty()) {
                                for (PdsMonitorTaskRange standard : standardList) {
                                    Long standardId = standard.getStandardId();
                                    PdsCategoryStandard categoryStandard = pdsCategoryStandardMapper.selectById(standardId);
                                    CategoryStandardVO categoryStandardVO = new CategoryStandardVO();
                                    BeanUtils.copyProperties(categoryStandard, categoryStandardVO);
                                    List<CategoryStandardVO> standardVOList = smallCategoryVO.getChildren();
                                    if (Objects.isNull(standardVOList)) {
                                        standardVOList = new ArrayList<>();
                                    }
                                    standardVOList.add(categoryStandardVO);
                                    smallCategoryVO.setChildren(standardVOList);
                                }
                            }
                            List<SmallCategoryVO> smallVOList = largeCategoryVO.getChildren();
                            if (Objects.isNull(smallVOList)) {
                                smallVOList = new ArrayList<>();
                            }
                            smallVOList.add(smallCategoryVO);
                            largeCategoryVO.setChildren(smallVOList);
                        }
                    }
                }
            }
        }
        return list;
    }
}
